package com.changhong.framework.sdfs.namenode.server.builder;

import com.changhong.framework.sdfs.namenode.server.exception.NameNodeException;

/**
 * @author summer
 */
public class NameNodeBuilder {

    /**
     * 通过Google DI框架 来获取相关的对象
     */
    private static volatile Injector nn_injector;
    private static final Object lock = new Object();

    private static Injector getInjector() {
        if (nn_injector == null) {
            synchronized (lock) {
                if (nn_injector == null) {
                    try {
                        nn_injector = ServiceManager.loadHead(Injector.class);
                    } catch (Throwable ex) {
                        NameNodeException exception = new NameNodeException("Unable to initialize Injector!", ex);
                        throw exception;
                    }
                }
            }
        }

        return nn_injector;
    }

    public static <T> T getInstance(Class<T> clazz) {
        try {
            return getInjector().getInstance(clazz);
        } catch (Throwable ex) {
            throw new NameNodeException(String.format("Unable to load instance for type %s!", clazz.getName()), ex);
        }
    }

}
